package com.yunxi.service.tms.dubbo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.bizToHub.pojo.CanSendResourcesVO;
import com.yunxi.bizToHub.pojo.CarRoadVO;
import com.yunxi.bizToHub.pojo.DespatchOrderAuditVO;
import com.yunxi.bizToHub.service.DespatchOrderDubboService;
import com.yunxi.core.base.Constant;
import com.yunxi.core.util.HttpUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.eai.EaiLog;
import com.yunxi.rocketmq.producer.EaiLogsProducer;
import com.yunxi.service.ReturnResultMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/12.
 */
public class DespatchOrderDubboServiceImpl implements DespatchOrderDubboService {

    protected static final Logger LOGGER = LoggerFactory.getLogger(DespatchOrderDubboServiceImpl.class);
    @Autowired
    private EaiLogsProducer eaiLogsProducer;
    @Value("${esb.switch}")
    private String esbSwitch;
    @Value("${TMS.URL}")
    private String TMS_URL;

    /**
     * 发运单审核信息
     * @param order
     * @return
     */
    @Override
    public ApiResult despatchOrderAuditPms2Tms(List<DespatchOrderAuditVO> order) {
        String despatchOrderAudit = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(despatchOrderAudit);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送发运单审核信息给TMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_TMS_DESPATCH_ORDER_AUDIT);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(despatchOrderAudit, Constant.PMS_TMS_DESPATCH_ORDER_AUDIT, Constant.PMS, TMS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(TMS_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));

            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("zstat")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }

    /**
     * 可发资源信息
     * @param order
     * @return
     */
    @Override
    public ApiResult canSendResourcesPms2Tms(List<CanSendResourcesVO>  order) {
        String canSendResources = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(canSendResources);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS系统发送可发资源信息给TMS系统");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_TMS_CAN_SEND_RESOURCE);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok();
        }
        try{
            Map map = HttpUtil.sendPost(canSendResources, Constant.PMS_TMS_CAN_SEND_RESOURCE, Constant.PMS, TMS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(TMS_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (esbSwitch.equals("false")) {
                return ResponseUtil.ok();
            }
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("zstat")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }

    @Override
    public ApiResult verifyCarRoadPms2Tms(CarRoadVO order) {
        String carRoad = JSONObject.toJSONString(order);
        ApiResult apiResult=null;
        EaiLog eaiLog=new EaiLog();
        eaiLog.setPmsEaiContent(carRoad);
        eaiLog.setRequestSendTime(new Date());
        eaiLog.setFlowDesc("PMS确认TMS系统小车道次信息");
        eaiLog.setSourceSystem(Constant.PMS);
        eaiLog.setServiceName(Constant.PMS_TMS_CAN_ROAD);
        if (esbSwitch.equals("false")) {
            return ResponseUtil.ok(ReturnResultMsg.verifyCarRoadPms2Tms());
        }
        try{
            Map map = HttpUtil.sendPost(carRoad, Constant.PMS_TMS_CAN_ROAD, Constant.PMS, TMS_URL);
            eaiLog.setRequestId(String.valueOf(map.get("requestId")));
            eaiLog.setResponseReceiveTime(new Date());
            eaiLog.setEsbResultContent(JSONObject.toJSONString(map));
            eaiLog.setUrl(TMS_URL);
            Object object = map.get("header");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object));
            String statusFlag=String.valueOf(jsonObject.get("statusFlag"));
            if (statusFlag.equals("0")){//这里只对ESB的处理状态做判断
                //处理失败，更状态位失败
                eaiLog.setDisposeStatus(2);
                apiResult= ResponseUtil.fail();
            }else {
                //将返回的数据返给PMS
                JSONObject jsonResponse = JSONObject.parseObject(String.valueOf(map.get("response")));
                if (String.valueOf(jsonResponse.get("zstat")).equals("E")){
                    //处理失败，更状态位失败
                    eaiLog.setDisposeStatus(2);
                    apiResult=ResponseUtil.fail();
                }else {
                    apiResult = ResponseUtil.ok(map.get("response"));
                }
            }
        }catch (Exception e){
            //处理失败，更状态位失败
            eaiLog.setDisposeStatus(2);
            LOGGER.error(e.getMessage(),e);
            LOGGER.error("调用ESB系统失败===========");
            apiResult=ResponseUtil.fail();
        }
        try {
            eaiLogsProducer.sendMessage(JSONObject.toJSONString(eaiLog));
        }catch (Exception e){
            LOGGER.error(e.getMessage(),e);
            apiResult=ResponseUtil.fail();
        }
        return apiResult;
    }
}
